package com.AAL.Action;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.AAL.DAO.ActionOnStockDAO;
import com.AAL.DAO.LogForStockDAO;
import com.AAL.DAO.PriceOfProductInStockDAO;
import com.AAL.DAO.ProductInStockDAO;
import com.AAL.DAO.RegionDAO;
import com.AAL.DAO.RequestingDetailDAO;
import com.AAL.DAO.RequestingDetailsForTransferingDAO;
import com.AAL.DAO.SourcingRequestDAO;
import com.AAL.DAO.StatusOfTransferingRequestDAO;
import com.AAL.DAO.TransferingRequestDAO;
import com.AAL.DAO.WarehouseDAO;
import com.AAL.Model.Account;
import com.AAL.Model.Employee;
import com.AAL.Model.LogDetailsForStock;
import com.AAL.Model.LogForStock;
import com.AAL.Model.PriceOfProductInStock;
import com.AAL.Model.ProductsInStock;
import com.AAL.Model.Region;
import com.AAL.Model.RequestingDetail;
import com.AAL.Model.RequestingDetailsForTransfering;
import com.AAL.Model.SourcingRequest;
import com.AAL.Model.StatusOfTransferingRequest;
import com.AAL.Model.TransferingRequest;
import com.AAL.Model.WareHouse;
import com.AAL.Model.WheelDetail;
import com.AAL.Utils.DateUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class TransferRequestAction extends ActionSupport implements	SessionAware {
	private Map<String, Object> session;
	private TransferingRequest transferingRequest;
	private List<TransferingRequest> listTransferingRequests;
	private TransferingRequestDAO transferingRequestDAO;
	private List<RequestingDetailsForTransfering> listRequestingDetailsForTransferings; 
	private RequestingDetailsForTransferingDAO  requestingDetailsForTransferingDAO;
	private List<StatusOfTransferingRequest> listStatusOfTransferingRequest;
	private List<RequestingDetail> lstRequestingDetail;
	private List<TransferingRequest> listTransferingRequestSearch;
	private List<ProductsInStock> listProductsInStocks;
	private List<ProductsInStock> listProductsInStocks1;
	private StatusOfTransferingRequestDAO statusOfTransferingRequestDAO;
	private ProductInStockDAO productInStockDAO;
	private Timestamp writingDateStart;
	private Timestamp writingDateEnd;
	private Timestamp requirementDateStart;
	private Timestamp requirementDateEnd;	
	private String transferingRequestIDs;
	private String statusOfTransferingRequestIDs;
	private String[] lstItemChoised;
	private String wareHouseChose;	
	private RequestingDetailDAO requestingDetailDAO;
	private List<Region> listRegion;
	private RegionDAO regionDAO;
	private WarehouseDAO warehouseDAO;
	private SourcingRequestDAO sourcingRequestDAO;	
	private PriceOfProductInStockDAO priceOfProductInStockDAO;
	private ActionOnStockDAO actionOnStockDAO;
	private LogForStockDAO logForStockDAO;
	public List<Region> getListRegion() {
		return listRegion;
	}

	public void setListRegion(List<Region> listRegion) {
		this.listRegion = listRegion;
	}

	public List<ProductsInStock> getListProductsInStocks1() {
		return listProductsInStocks1;
	}

	public void setListProductsInStocks1(List<ProductsInStock> listProductsInStocks1) {
		this.listProductsInStocks1 = listProductsInStocks1;
	}

	public List<RequestingDetail> getLstRequestingDetail() {
		return lstRequestingDetail;
	}

	public void setLstRequestingDetail(List<RequestingDetail> lstRequestingDetail) {
		this.lstRequestingDetail = lstRequestingDetail;
	}

	public String getWareHouseChose() {
		return wareHouseChose;
	}

	public void setWareHouseChose(String wareHouseChose) {
		this.wareHouseChose = wareHouseChose;
	}

	public List<ProductsInStock> getListProductsInStocks() {
		return listProductsInStocks;
	}

	public void setListProductsInStocks(List<ProductsInStock> listProductsInStocks) {
		this.listProductsInStocks = listProductsInStocks;
	}

	public String[] getLstItemChoised() {
		return lstItemChoised;
	}

	public void setLstItemChoised(String[] lstItemChoised) {
		this.lstItemChoised = lstItemChoised;
	}

	public List<RequestingDetailsForTransfering> getListRequestingDetailsForTransferings() {
		return listRequestingDetailsForTransferings;
	}

	public void setListRequestingDetailsForTransferings(
			List<RequestingDetailsForTransfering> listRequestingDetailsForTransferings) {
		this.listRequestingDetailsForTransferings = listRequestingDetailsForTransferings;
	}

	public List<TransferingRequest> getListTransferingRequestSearch() {
		return listTransferingRequestSearch;
	}

	public void setListTransferingRequestSearch(
			List<TransferingRequest> listTransferingRequestSearch) {
		this.listTransferingRequestSearch = listTransferingRequestSearch;
	}

	public Timestamp getWritingDateStart() {
		return writingDateStart;
	}

	public void setWritingDateStart(Timestamp writingDateStart) {
		this.writingDateStart = writingDateStart;
	}

	public Timestamp getWritingDateEnd() {
		return writingDateEnd;
	}

	public void setWritingDateEnd(Timestamp writingDateEnd) {
		this.writingDateEnd = writingDateEnd;
	}

	public Timestamp getRequirementDateStart() {
		return requirementDateStart;
	}

	public void setRequirementDateStart(Timestamp requirementDateStart) {
		this.requirementDateStart = requirementDateStart;
	}

	public Timestamp getRequirementDateEnd() {
		return requirementDateEnd;
	}

	public void setRequirementDateEnd(Timestamp requirementDateEnd) {
		this.requirementDateEnd = requirementDateEnd;
	}

	public String getTransferingRequestIDs() {
		return transferingRequestIDs;
	}

	public void setTransferingRequestIDs(String transferingRequestIDs) {
		this.transferingRequestIDs = transferingRequestIDs;
	}

	public String getStatusOfTransferingRequestIDs() {
		return statusOfTransferingRequestIDs;
	}

	public void setStatusOfTransferingRequestIDs(
			String statusOfTransferingRequestIDs) {
		this.statusOfTransferingRequestIDs = statusOfTransferingRequestIDs;
	}

	public StatusOfTransferingRequestDAO getStatusOfTransferingRequestDAO() {
		return statusOfTransferingRequestDAO;
	}

	public void setStatusOfTransferingRequestDAO(
			StatusOfTransferingRequestDAO statusOfTransferingRequestDAO) {
		this.statusOfTransferingRequestDAO = statusOfTransferingRequestDAO;
	}

	public List<StatusOfTransferingRequest> getListStatusOfTransferingRequest() {
		return listStatusOfTransferingRequest;
	}

	public void setListStatusOfTransferingRequest(
			List<StatusOfTransferingRequest> listStatusOfTransferingRequest) {
		this.listStatusOfTransferingRequest = listStatusOfTransferingRequest;
	}


	public TransferingRequestDAO getTransferingRequestDAO() {
		return transferingRequestDAO;
	}

	public void setTransferingRequestDAO(TransferingRequestDAO transferingRequestDAO) {
		this.transferingRequestDAO = transferingRequestDAO;
	}
	

	public TransferingRequest getTransferingRequest() {
		return transferingRequest;
	}

	public void setTransferingRequest(TransferingRequest transferingRequest) {
		this.transferingRequest = transferingRequest;
	}

	public List<TransferingRequest> getListTransferingRequests() {
		return listTransferingRequests;
	}	

	public void setListTransferingRequests(
			List<TransferingRequest> listTransferingRequests) {
		this.listTransferingRequests = listTransferingRequests;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public TransferRequestAction() {
		this.transferingRequestDAO = new TransferingRequestDAO();		
		this.statusOfTransferingRequestDAO = new StatusOfTransferingRequestDAO();
		this.productInStockDAO = new ProductInStockDAO();
		this.requestingDetailsForTransferingDAO = new RequestingDetailsForTransferingDAO();
		this.requestingDetailDAO = new RequestingDetailDAO();
		this.regionDAO = new RegionDAO();
		this.warehouseDAO= new WarehouseDAO();
		this.sourcingRequestDAO = new SourcingRequestDAO();
		this.priceOfProductInStockDAO = new PriceOfProductInStockDAO();
		this.actionOnStockDAO = new ActionOnStockDAO();
		this.logForStockDAO = new LogForStockDAO();
	}
	//phatdt view list start	
	public String transferRequestManagement() {
		try {
		System.out.println("goi quan li phieu dieu phoi");
		setListTransferingRequests(transferingRequestDAO.getAllTransferingRequests());
		setListStatusOfTransferingRequest(statusOfTransferingRequestDAO.getAllStatusOfTransferingRequests());

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	//phatdt view list end
	//phatdt view detail start
	public String viewTransferRequestDetail() {
		try {
		System.out.println("goi chi tiet dieu phoi");
		HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		String transferingRequestID = request.getParameter("id");
		setTransferingRequest(transferingRequestDAO.getTransferingRequest(transferingRequestID));
		session.put("transferingRequestSession", transferingRequest);
		setListRequestingDetailsForTransferings(requestingDetailsForTransferingDAO.GetTransferRequestingDetailByTransferingRequestID(transferingRequestID));
		session.put("listRequestingDetailsForTransferingsSession", listRequestingDetailsForTransferings);

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	//phatdt view detail end
	public String viewProcessTransferRequest() {
		try {
		System.out.println("goi view xử lý dieu phoi");
		
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	public String processTransferRequest_Manager() {
		try {
		System.out.println("goi xu ly dieu phoi");
		HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		String transferingRequestIDTemp = request.getParameter("transferingRequestID").toString();
		int statusIDTmp = Integer.parseInt(request.getParameter("statusProcess").toString());
		System.out.println("transferingRequestID:statusID:"+transferingRequestIDTemp+"statusID:"+statusIDTmp);
		StatusOfTransferingRequest statusOfTransferingRequestTemp = statusOfTransferingRequestDAO.getStatusOfTransferingRequest(statusIDTmp);
		//System.out.println("");
		TransferingRequest transferingRequestTemp = transferingRequestDAO.getTransferingRequest(transferingRequestIDTemp);
		transferingRequestTemp.setStatusOfTransferingRequest(statusOfTransferingRequestTemp);
		Account myAccount = (Account) session.get("myAccount");
		transferingRequestTemp.setEmployeeByManager(myAccount.getEmployee());
		transferingRequestTemp.setProcessingDate(DateUtil.getToday());
		System.out.println("id"+transferingRequestTemp.getTransferingRequestID());
		System.out.println("mana"+transferingRequestTemp.getEmployeeByManager());
		System.out.println("id"+transferingRequestTemp.getRequestingDetailsForTransfering());
		this.transferingRequestDAO.saveAndUpdateTransferingRequest(transferingRequestTemp);
		setTransferingRequest(transferingRequestDAO.getTransferingRequest(transferingRequestIDTemp));
		setListRequestingDetailsForTransferings(requestingDetailsForTransferingDAO.GetTransferRequestingDetailByTransferingRequestID(transferingRequestIDTemp));

		
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	
	//phatdt view search start
	public String viewTransferRequestSearch() {
		try {
		System.out.println("goi tìm dieu phoi");
		System.out.println(""+transferingRequestIDs+"  "+statusOfTransferingRequestIDs+" "+writingDateStart+"  "+writingDateEnd+"  "+requirementDateStart+"  "+requirementDateEnd);
		if(writingDateStart == null){
			String date = "1/1/1900";
			writingDateStart = DateUtil.convertStringToTimestamp(date);
			System.out.println("w: "+writingDateStart);
		}
		if(requirementDateStart == null){
			String date = "1/1/1900";
			requirementDateStart = DateUtil.convertStringToTimestamp(date);
			System.out.println("w: "+requirementDateStart);
		}
		if(writingDateEnd == null){
			writingDateEnd=DateUtil.getToday();
			System.out.println("we "+writingDateEnd);
		}
		if(requirementDateEnd == null){
			requirementDateEnd=DateUtil.getToday();
			System.out.println("re: "+requirementDateEnd);
		}
		setListTransferingRequestSearch(transferingRequestDAO.getAllTransferingRequestsSearch(transferingRequestIDs, statusOfTransferingRequestIDs, writingDateStart, writingDateEnd, requirementDateStart, requirementDateEnd));
		setListStatusOfTransferingRequest(statusOfTransferingRequestDAO.getAllStatusOfTransferingRequests());
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	//phatdt view search end
	public String trangsferRequestViewAdd() {
		try {
		System.out.println("goi view them");
		HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		String transferingRequestID = request.getParameter("id");
		setTransferingRequest(transferingRequestDAO.getTransferingRequest(transferingRequestID));
		setListRequestingDetailsForTransferings(requestingDetailsForTransferingDAO.GetTransferRequestingDetailByTransferingRequestID(transferingRequestID));

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	public String ProcessSourcingRequest2TransferRequest_1() {	
		System.out.println("wareHouseChose:"+wareHouseChose);		
		try {
			List<RequestingDetail> lstRequestingDetailTemp = null;
			List<RequestingDetail> lstRequestingDetail2 = new ArrayList<RequestingDetail>();
			System.out.println("goi view them" + lstItemChoised);
			session.put("SourcingRequestchosed", lstItemChoised);
			if (lstItemChoised != null && lstItemChoised.length != 0) {
				System.out.println("You have selected: ");
				int vtri=-1;
				for (int i = 0; i < lstItemChoised.length; i++) {
					System.out.println(lstItemChoised[i]);
					lstRequestingDetailTemp = this.requestingDetailDAO.GetRequestingDetailBySourcingRequestID(lstItemChoised[i].toString());
					for (RequestingDetail item : lstRequestingDetailTemp){
						if(lstRequestingDetail2.size()!=0){
							for (RequestingDetail item1 : lstRequestingDetail2){
								if(item.getWheelDetail().getWheelDetailID().equals(item1.getWheelDetail().getWheelDetailID())){
									int soluongt= item.getAmount()+item1.getAmount();
									item.setAmount((short) soluongt);
									 vtri = lstRequestingDetailTemp.indexOf(item);
									lstRequestingDetail2.set(vtri, item);
								}
							}
						}
						if(vtri==-1){
							lstRequestingDetail2.add(item);
						}						
						System.out.println("phat:"+item.getRequestingDetailID()+"item.getSourcingRequestID():"+item.getSourcingRequest().getSourcingRequestID());
					 }
					
				}		
		
				setLstRequestingDetail(lstRequestingDetail2);
				
			session.put("lstRequestingDetailSesion", lstRequestingDetail);
				}
			if(wareHouseChose.equals("1")){
				setListProductsInStocks(productInStockDAO.getLstProductsInStocksByWareHouseID(Short.parseShort("2")));
				setListProductsInStocks1(productInStockDAO.getLstProductsInStocksByWareHouseID(Short.parseShort("3")));
				setListRegion(regionDAO.getLstRegionByRegionID(Short.parseShort("2"), Short.parseShort("3")));
				wareHouseChose="kho miền Bắc";
			}
			if(wareHouseChose.equals("2")){
				setListProductsInStocks(productInStockDAO.getLstProductsInStocksByWareHouseID(Short.parseShort("1")));
				setListProductsInStocks1(productInStockDAO.getLstProductsInStocksByWareHouseID(Short.parseShort("3")));
				setListRegion(regionDAO.getLstRegionByRegionID(Short.parseShort("1"), Short.parseShort("3")));
				wareHouseChose="kho miền Trung";
			}
			if(wareHouseChose.equals("3")){
				setListProductsInStocks(productInStockDAO.getLstProductsInStocksByWareHouseID(Short.parseShort("1")));
				setListProductsInStocks1(productInStockDAO.getLstProductsInStocksByWareHouseID(Short.parseShort("2")));
				setListRegion(regionDAO.getLstRegionByRegionID(Short.parseShort("1"), Short.parseShort("2")));
				wareHouseChose="kho miền Nam";
			}
			//this.setListRegion(regionDAO.getALlList());
			
			} catch (Exception e) {
				 System.out.println("ek:"+e);
			return ERROR;
		}
		return SUCCESS;		
	
	}
	@SuppressWarnings("unchecked")
	public String ProcessSourcingRequest2TransferRequest_2() {
		try {
		Account myAccount = null;
		if (session.get("myAccount") != null)
		  myAccount = (Account) session.get("myAccount");
		Employee employee = myAccount.getEmployee();
		System.out.println("goi view them ProcessSourcingRequest2TransferRequest_2");
		List<RequestingDetail> lstRequestingDetailSession = (List<RequestingDetail>) session.get("lstRequestingDetailSesion");
	    List<RequestingDetailsForTransfering> listRequestingDetailsForTransferingsTemp = new ArrayList<RequestingDetailsForTransfering>();
		System.out.println("lstRequestingDetail"+lstRequestingDetailSession.size());
		HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		String toWareHouse = request.getParameter("toWareHouse").toString();
		String requirementDateInput = request.getParameter("requirementDateInput").toString();
		String transferingRequestIDInput = "TR";
		int sumTR = transferingRequestDAO.getAllTransferingRequests().size()+1;
		transferingRequestIDInput+=sumTR;
		StatusOfTransferingRequest statusOfTransferingRequest = statusOfTransferingRequestDAO.getStatusOfTransferingRequest(1);
		int wareHouseChoseRQ;
		Short amoutChoseRQ;
		
		TransferingRequest transferingRequest1 = new TransferingRequest();		
		
			transferingRequest1.setTransferingRequestID(transferingRequestIDInput);
			transferingRequest1.setToWareHouse(warehouseDAO.getWareHouse(Integer.parseInt(toWareHouse)));
			transferingRequest1.setEmployeeByCoordinator(employee);
			transferingRequest1.setWritingDate(DateUtil.getToday());
			transferingRequest1.setRequirementDate(DateUtil.convertStringToTimestamp(requirementDateInput));
			transferingRequest1.setStatusOfTransferingRequest(statusOfTransferingRequest);
			for(RequestingDetail item : lstRequestingDetailSession){
				wareHouseChoseRQ = Integer.parseInt(request.getParameter("wareHouseChose"+item.getWheelDetail().getWheelDetailID()).toString());
				amoutChoseRQ = Short.parseShort(request.getParameter("amoutchose"+item.getWheelDetail().getWheelDetailID()).toString());			
				System.out.println("ma sp:"+item.getWheelDetail().getWheelDetailID());
				System.out.println("Kho xu ly:"+wareHouseChoseRQ);
				System.out.println("So luong:"+amoutChoseRQ);				
				RequestingDetailsForTransfering requestingDetailsForTransferingTemp = new RequestingDetailsForTransfering();
				requestingDetailsForTransferingTemp.setWheelDetail(item.getWheelDetail());
				requestingDetailsForTransferingTemp.setAmount(amoutChoseRQ);
				requestingDetailsForTransferingTemp.setFromWarehouse(warehouseDAO.getWareHouse(wareHouseChoseRQ));
				listRequestingDetailsForTransferingsTemp.add(requestingDetailsForTransferingTemp);
				
			}
			transferingRequest1.setRequestingDetailsForTransfering(listRequestingDetailsForTransferingsTemp);
			String SourcingRequestchosed []=(String[]) session.get("SourcingRequestchosed");
			List<SourcingRequest> lstSourcingRequest = new ArrayList<SourcingRequest>();
			for(int i=0;i<SourcingRequestchosed.length;i++){
				lstSourcingRequest.add(sourcingRequestDAO.getSourcingRequest(SourcingRequestchosed[i]));
			}
			System.out.println("tao du lieu thanh cong");
			transferingRequestDAO.saveAndUpdateTransferingRequest(transferingRequest1, lstSourcingRequest);
			System.out.println("Luu data thanh cong");
			
			session.remove("lstRequestingDetail");		
			setTransferingRequest(transferingRequestDAO.getTransferingRequest(transferingRequestIDInput));
			setListRequestingDetailsForTransferings(requestingDetailsForTransferingDAO.GetTransferRequestingDetailByTransferingRequestID(transferingRequestIDInput));

	
		
		
		
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	@SuppressWarnings("unchecked")
	public String Coodinate_process() {
		try {
		System.out.println("goi Coodinate_process");		
		TransferingRequest transferingRequestSession = (TransferingRequest) session.get("transferingRequestSession");
		List<RequestingDetailsForTransfering> listRequestingDetailsForTransferingsSession =	(List<RequestingDetailsForTransfering>) session.get("listRequestingDetailsForTransferingsSession");
		LogForStock logForStockInput = new LogForStock();
		LogForStock logForStockOutput1 = new LogForStock();
		LogForStock logForStockOutput2 = new LogForStock();
		List<LogDetailsForStock> listLogDetailsForStocksInput = new ArrayList<LogDetailsForStock>();
		List<LogDetailsForStock> listLogDetailsForStocksOutput1 = new ArrayList<LogDetailsForStock>();
		List<LogDetailsForStock> listLogDetailsForStocksOutput2 = new ArrayList<LogDetailsForStock>();
		logForStockInput.setActionOnStock(actionOnStockDAO.getActionOnStock(1));
		logForStockInput.setIsApproved(false);
		logForStockInput.setTransferingRequest(transferingRequestSession);
		logForStockInput.setWritingDate(DateUtil.getToday());
		logForStockInput.setWareHous(transferingRequestSession.getToWareHouse());
		logForStockOutput1.setActionOnStock(actionOnStockDAO.getActionOnStock(2));
		logForStockOutput1.setIsApproved(false);
		logForStockOutput1.setTransferingRequest(transferingRequestSession);
		logForStockOutput1.setWritingDate(DateUtil.getToday());		
		logForStockOutput2.setActionOnStock(actionOnStockDAO.getActionOnStock(2));
		logForStockOutput2.setIsApproved(false);
		logForStockOutput2.setTransferingRequest(transferingRequestSession);
		logForStockOutput2.setWritingDate(DateUtil.getToday());
		System.out.println(""+logForStockInput.getActionOnStock().getActionOnStockID());
		System.out.println(""+logForStockInput.getTransferingRequest().getTransferingRequestID());
		WareHouse wareHouseOut1=null;
		WareHouse wareHouseOut2=null;
		for(RequestingDetailsForTransfering item: listRequestingDetailsForTransferingsSession){
			LogDetailsForStock logDetailsForStockTemp = new LogDetailsForStock();
			LogDetailsForStock logDetailsForStockTemp1 = new LogDetailsForStock();
			logDetailsForStockTemp.setWheelDetail(item.getWheelDetail());
			logDetailsForStockTemp.setAmountOfInventory((short) productInStockDAO.getProductsInStockByWheelDetailIDWareHouseID(item.getWheelDetail().getWheelDetailID(), (short) 2).getRealAmount());
			logDetailsForStockTemp.setInvoiceAmount(item.getAmount());
			
			//logDetailsForStockTemp.setUnitPriceOfInventory(priceOfProductInStockDAO.getPriceOfProductInStockByWheelDetailID(item.getWheelDetail().getWheelDetailID()).getUnitPrice());
			//Vi chua co du lieu gia nen set cung			
			logDetailsForStockTemp.setUnitPriceOfInventory(new BigDecimal("50000.00"));
			logDetailsForStockTemp.setUnitPrice(new BigDecimal("50000.00"));
			listLogDetailsForStocksInput.add(logDetailsForStockTemp);
			
			if(transferingRequestSession.getToWareHouse().getWareHouseID()==1){
				if(item.getFromWarehouse().getWareHouseID()==2){
					logDetailsForStockTemp1.setWheelDetail(item.getWheelDetail());
					logDetailsForStockTemp1.setAmountOfInventory((short) productInStockDAO.getProductsInStockByWheelDetailIDWareHouseID(item.getWheelDetail().getWheelDetailID(), (short) 2).getRealAmount());
					logDetailsForStockTemp1.setInvoiceAmount(item.getAmount());
					//logDetailsForStockTemp1.setUnitPriceOfInventory(priceOfProductInStockDAO.getPriceOfProductInStockByWheelDetailID(item.getWheelDetail().getWheelDetailID()).getUnitPrice());
					//Vi chua co du lieu gia nen set cung					
					logDetailsForStockTemp1.setUnitPriceOfInventory(new BigDecimal("50000.00"));		
					logDetailsForStockTemp1.setUnitPrice(new BigDecimal("50000.00"));
					listLogDetailsForStocksOutput1.add(logDetailsForStockTemp1);	
					if(wareHouseOut1==null)wareHouseOut1=item.getFromWarehouse();
					
				}
				if(item.getFromWarehouse().getWareHouseID()==3){
					logDetailsForStockTemp1.setWheelDetail(item.getWheelDetail());
					logDetailsForStockTemp1.setAmountOfInventory((short) productInStockDAO.getProductsInStockByWheelDetailIDWareHouseID(item.getWheelDetail().getWheelDetailID(), (short) 2).getRealAmount());
					logDetailsForStockTemp1.setInvoiceAmount(item.getAmount());
					//logDetailsForStockTemp1.setUnitPriceOfInventory(priceOfProductInStockDAO.getPriceOfProductInStockByWheelDetailID(item.getWheelDetail().getWheelDetailID()).getUnitPrice());
					//Vi chua co du lieu gia nen set cung
					
					logDetailsForStockTemp1.setUnitPriceOfInventory(new BigDecimal("50000.00"));
					logDetailsForStockTemp1.setUnitPrice(new BigDecimal("50000.00"));
					listLogDetailsForStocksOutput2.add(logDetailsForStockTemp1);
					if(wareHouseOut2==null)wareHouseOut2=item.getFromWarehouse();
					
				}
			}
			if(transferingRequestSession.getToWareHouse().getWareHouseID()==2){
				if(item.getFromWarehouse().getWareHouseID()==1){
					logDetailsForStockTemp1.setWheelDetail(item.getWheelDetail());
					logDetailsForStockTemp1.setAmountOfInventory((short) productInStockDAO.getProductsInStockByWheelDetailIDWareHouseID(item.getWheelDetail().getWheelDetailID(), (short) 2).getRealAmount());
					logDetailsForStockTemp1.setInvoiceAmount(item.getAmount());
					//logDetailsForStockTemp1.setUnitPriceOfInventory(priceOfProductInStockDAO.getPriceOfProductInStockByWheelDetailID(item.getWheelDetail().getWheelDetailID()).getUnitPrice());
					//Vi chua co du lieu gia nen set cung
					
					logDetailsForStockTemp1.setUnitPriceOfInventory(new BigDecimal("50000.00"));
					logDetailsForStockTemp1.setUnitPrice(new BigDecimal("50000.00"));
					listLogDetailsForStocksOutput1.add(logDetailsForStockTemp1);
					if(wareHouseOut1==null)wareHouseOut1=item.getFromWarehouse();
					
				}
				if(item.getFromWarehouse().getWareHouseID()==3){
					logDetailsForStockTemp1.setWheelDetail(item.getWheelDetail());
					logDetailsForStockTemp1.setAmountOfInventory((short) productInStockDAO.getProductsInStockByWheelDetailIDWareHouseID(item.getWheelDetail().getWheelDetailID(), (short) 2).getRealAmount());
					logDetailsForStockTemp1.setInvoiceAmount(item.getAmount());
					//logDetailsForStockTemp1.setUnitPriceOfInventory(priceOfProductInStockDAO.getPriceOfProductInStockByWheelDetailID(item.getWheelDetail().getWheelDetailID()).getUnitPrice());
					//Vi chua co du lieu gia nen set cung
					
					logDetailsForStockTemp1.setUnitPriceOfInventory(new BigDecimal("50000.00"));
					logDetailsForStockTemp1.setUnitPrice(new BigDecimal("50000.00"));
					listLogDetailsForStocksOutput2.add(logDetailsForStockTemp1);
					if(wareHouseOut2==null)wareHouseOut2=item.getFromWarehouse();
					
				}
			}
			if(transferingRequestSession.getToWareHouse().getWareHouseID()==3){
				if(item.getFromWarehouse().getWareHouseID()==1){
					logDetailsForStockTemp1.setWheelDetail(item.getWheelDetail());
					logDetailsForStockTemp1.setAmountOfInventory((short) productInStockDAO.getProductsInStockByWheelDetailIDWareHouseID(item.getWheelDetail().getWheelDetailID(), (short) 2).getRealAmount());
					logDetailsForStockTemp1.setInvoiceAmount(item.getAmount());
					//logDetailsForStockTemp1.setUnitPriceOfInventory(priceOfProductInStockDAO.getPriceOfProductInStockByWheelDetailID(item.getWheelDetail().getWheelDetailID()).getUnitPrice());
					//Vi chua co du lieu gia nen set cung
					
					logDetailsForStockTemp1.setUnitPriceOfInventory(new BigDecimal("50000.00"));
					logDetailsForStockTemp1.setUnitPrice(new BigDecimal("50000.00"));
					listLogDetailsForStocksOutput1.add(logDetailsForStockTemp1);
					if(wareHouseOut1==null)wareHouseOut1=item.getFromWarehouse();
					
				}
				if(item.getFromWarehouse().getWareHouseID()==2){
					logDetailsForStockTemp1.setWheelDetail(item.getWheelDetail());
					logDetailsForStockTemp1.setAmountOfInventory((short) productInStockDAO.getProductsInStockByWheelDetailIDWareHouseID(item.getWheelDetail().getWheelDetailID(), (short) 2).getRealAmount());
					logDetailsForStockTemp1.setInvoiceAmount(item.getAmount());
					//logDetailsForStockTemp1.setUnitPriceOfInventory(priceOfProductInStockDAO.getPriceOfProductInStockByWheelDetailID(item.getWheelDetail().getWheelDetailID()).getUnitPrice());
					//Vi chua co du lieu gia nen set cung
					
					logDetailsForStockTemp1.setUnitPriceOfInventory(new BigDecimal("50000.00"));
					logDetailsForStockTemp1.setUnitPrice(new BigDecimal("50000.00"));
					listLogDetailsForStocksOutput2.add(logDetailsForStockTemp1);
					if(wareHouseOut2==null)wareHouseOut2=item.getFromWarehouse();
					
				}
			}
		}
		logForStockInput.setLogDetailsForStocks(listLogDetailsForStocksInput);
		logForStockOutput1.setLogDetailsForStocks(listLogDetailsForStocksOutput1);
		logForStockOutput2.setLogDetailsForStocks(listLogDetailsForStocksOutput2);
		List<LogForStock> listLogForStocksSave= new ArrayList<LogForStock>();
		System.out.println(""+logForStockInput.getTransferingRequest().getTransferingRequestID());
		System.out.println(""+logForStockInput.getActionOnStock().getActionName());
		if(wareHouseOut1!=null && wareHouseOut2!=null){
			listLogForStocksSave.add(logForStockInput);
			logForStockOutput1.setWareHous(wareHouseOut1);
			listLogForStocksSave.add(logForStockOutput1);
			logForStockOutput2.setWareHous(wareHouseOut2);
			listLogForStocksSave.add(logForStockOutput2);	
			System.out.println(""+listLogForStocksSave.get(0).getTransferingRequest().getTransferingRequestID()+""+listLogForStocksSave.get(0).getActionOnStock().getActionOnStockID());
			System.out.println("thu"+listLogForStocksSave.get(0).getLogDetailsForStocks().size());
			logForStockDAO.saveAndUpdateLogForStockArray(listLogForStocksSave);
		}
		else {
			if(wareHouseOut1!=null){
				listLogForStocksSave.add(logForStockInput);
				logForStockOutput1.setWareHous(wareHouseOut1);
				listLogForStocksSave.add(logForStockOutput1);							
				logForStockDAO.saveAndUpdateLogForStockArray(listLogForStocksSave);
			}
			else{
				listLogForStocksSave.add(logForStockInput);	
				logForStockOutput2.setWareHous(wareHouseOut2);
				listLogForStocksSave.add(logForStockOutput2);				
				logForStockDAO.saveAndUpdateLogForStockArray(listLogForStocksSave);
			}
		}		
		StatusOfTransferingRequest statusOfTransferingRequestTemp = statusOfTransferingRequestDAO.getStatusOfTransferingRequest(5);
		transferingRequestSession.setStatusOfTransferingRequest(statusOfTransferingRequestTemp);
		//transferingRequestDAO.saveAndUpdateTransferingRequest(transferingRequestSession);
		System.out.println("luu data thanh cong");
		session.remove("transferingRequestSession");
		session.remove("listRequestingDetailsForTransferingsSession");
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	

}
